/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.security.spring.context;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoaderListener;

import javax.servlet.*;
import java.util.Arrays;

/**
 * 加载扩展的配置片段
 * 算了还是通过导入处理吧
 *
 * @author vacoor
 * @deprecated
 */
@Deprecated
public class VariousContextLoaderListener extends ContextLoaderListener implements ServletRequestListener {
    public static final String FRAGMENT_LOCATIONS_PARAM = "contextConfigFragmentLocations";
    private static final String DEFAULT_FRAGMENTS = "classpath*:spring-*-fragment.xml;classpath*:conf/spring-*-fragment.xml";
    private ServletContext servletContext;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        this.servletContext = event.getServletContext();
        super.contextInitialized(event);
    }

    @Override
    protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
        String[] locations = applicationContext.getConfigLocations();

        String fragments = servletContext.getInitParameter(FRAGMENT_LOCATIONS_PARAM);
        fragments = StringUtils.hasLength(fragments) ? fragments : DEFAULT_FRAGMENTS;
        String[] fragLocations = StringUtils.tokenizeToStringArray(fragments, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);

        String[] newLocations = Arrays.copyOf(locations, locations.length + fragLocations.length);
        System.arraycopy(fragLocations, 0, newLocations, locations.length, fragLocations.length);

        applicationContext.setConfigLocations(newLocations);
        super.customizeContext(servletContext, applicationContext);
    }

    @Override
    public void requestInitialized(ServletRequestEvent event) {
        ServletRequest request = event.getServletRequest();
    }

    @Override
    public void requestDestroyed(ServletRequestEvent event) {
        ServletRequest request = event.getServletRequest();

    }
}
